package com.hb.trade.server.project.file.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.core.pojo.project.file.dto.ReviewItemThirdDto;
import com.hb.core.pojo.project.file.entity.ReviewItemSecond;
import com.hb.core.pojo.project.file.entity.ReviewItemThird;
import com.hb.core.pojo.project.file.mapper.ReviewItemSecondMapper;
import com.hb.core.pojo.project.file.mapper.ReviewItemThirdMapper;
import com.hb.trade.server.project.file.service.ReviewItemThirdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.fzzn.core.util.IdUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 三级评审项 服务实现类
 * </p>
 *
 * @author tian
 * @since 2022-04-13
 */
@Service
public class ReviewItemThirdServiceImpl extends ServiceImpl<ReviewItemThirdMapper, ReviewItemThird> implements ReviewItemThirdService {

    @Resource
    private ReviewItemSecondMapper reviewItemSecondMapper;

    /**
     * 查询三级评审项
     *
     * @param reviewItemThirdDto
     * @return java.util.List<com.hb.core.pojo.project.file.dto.ReviewItemThirdDto>
     * @author tian
     * @date 2022-4-13 11:45
     */
    @Override
    public List<ReviewItemThirdDto> selectReviewItemThirdList(ReviewItemThirdDto reviewItemThirdDto) {
        return baseMapper.selectReviewItemThirdByFirstReviewItemId(reviewItemThirdDto.getFirstReviewItemId());
    }

    /**
     * 根据二级评审项id查询三级评审项信息
     * @param secondReviewItemId
     * @return
     */
    @Override
    public List<ReviewItemThirdDto> selectReviewItemThirdListBySecondReviewItemId(Long secondReviewItemId) {
        return baseMapper.selectReviewItemThirdBySecondReviewItemId(secondReviewItemId);
    }

    /**
     * 保存，修改三级评审项
     *
     * @param reviewItemThirdDto
     * @return java.lang.String
     * @author tian
     * @date 2022-4-13 11:48
     */
    @Override
    public String editReviewItemThird(ReviewItemThirdDto reviewItemThirdDto) {
        // 判断名称是否重复
        QueryWrapper<ReviewItemThird> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ReviewItemThird::getThirdReviewItemName, reviewItemThirdDto.getThirdReviewItemName())
                .eq(ReviewItemThird::getSecondReviewItemId, reviewItemThirdDto.getSecondReviewItemId());
        if(null != reviewItemThirdDto.getId()){
            queryWrapper.lambda().ne(ReviewItemThird::getId, reviewItemThirdDto.getId());
        }
        List<ReviewItemThird> reviewItemThirdList = baseMapper.selectList(queryWrapper);
        if(!CollectionUtil.isEmpty(reviewItemThirdList)){
            return "评审项名称不允许重复，操作失败";
        }

        // 查询二级评审项是否是定量  如果是定量计算打分范围
        ReviewItemSecond reviewItemSecond = reviewItemSecondMapper.selectById(reviewItemThirdDto.getSecondReviewItemId());
        if(null != reviewItemSecond && reviewItemSecond.getReviewMode().equals(1) && StringUtils.isNotEmpty(reviewItemSecond.getScoreRange())){
            BigDecimal scoreRange = new BigDecimal(reviewItemSecond.getScoreRange());
            BigDecimal assessScore = new BigDecimal(reviewItemThirdDto.getAssessScore());
            BigDecimal lowestScore = scoreRange.divide(new BigDecimal("100")).multiply(assessScore).setScale(2,BigDecimal.ROUND_HALF_UP);
            reviewItemThirdDto.setLowestScore(lowestScore.toString());
        }else{
            reviewItemThirdDto.setLowestScore("0");
        }

        ReviewItemThird reviewItemThird = new ReviewItemThird();
        reviewItemThirdDto.to(reviewItemThird);
        Integer num = 0;
        if (null != reviewItemThird.getId()) {
            reviewItemThird.setUpdateId(reviewItemThirdDto.getCreateId());
            reviewItemThird.setUpdateName(reviewItemThirdDto.getCreateName());
            reviewItemThird.setUpdateTime(LocalDateTime.now());
            num = baseMapper.updateById(reviewItemThird);
        } else {
            // 根据二级评审单查询最大的排序
            Integer sort = baseMapper.selectSortBySecondReviewItemId(reviewItemThirdDto.getSecondReviewItemId());

            reviewItemThird.setId(IdUtil.getId());
            reviewItemThird.setCreateId(reviewItemThirdDto.getCreateId());
            reviewItemThird.setCreateName(reviewItemThirdDto.getCreateName());
            reviewItemThird.setCreateTime(LocalDateTime.now());
            reviewItemThird.setSort(sort + 1);
            num = baseMapper.insert(reviewItemThird);
        }
        return num > 0 ? reviewItemThird.getId().toString() : "操作失败";
    }

    /**
     * 删除三级评审项
     *
     * @param reviewItemThirdDto
     * @return java.lang.Boolean
     * @author tian
     * @date 2022-4-13 11:49
     */
    @Override
    public Boolean deleteReviewItemThird(ReviewItemThirdDto reviewItemThirdDto) {
        int num = baseMapper.deleteById(reviewItemThirdDto.getId());
        return num > 0 ? true : false;
    }
}
